/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import Utility.VarcharUpdater;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import vo.rb_purchase;
import vo.rb_raw_mat_stock;

/**
 *
 * @author Shuhail
 */
public class rb_raw_mat_stock_persist {
    Session sess=null;
    Transaction tx=null;
    
    public static rb_raw_mat_stock_persist getInstance() {
        return new rb_raw_mat_stock_persist();
    }

    public rb_raw_mat_stock_persist() {
    SessionFactory sf=new Configuration().configure().buildSessionFactory();
    sess=sf.openSession();
    tx=sess.beginTransaction();
    }
    
    
    public boolean insert(rb_raw_mat_stock stock) {
        rb_raw_mat_stock stock_new =new rb_raw_mat_stock();
        
        stock_new.setCurrent_stock(stock.getCurrent_stock());
        stock_new.setCurrent_unit_price(stock.getCurrent_unit_price());
        stock_new.setCurrent_value(stock.getCurrent_value());
        stock_new.setLast_updated_date(stock.getLast_updated_date());
        stock_new.setLast_updated_invoice_no(stock.getLast_updated_invoice_no());
        stock_new.setRaw_mat_id(stock.getRaw_mat_id());
        
        
        sess.save(stock_new);
        tx.commit();
        
        return true;
    }
    
    public boolean update(rb_purchase purchase){
        
        System.out.println("Updating RawMaterial Stock of RawMat - "+purchase.getRaw_mat_id()+" on " +purchase.getDate());
        rb_raw_mat_stock updatedStock = new rb_raw_mat_stock();
        String lastPK = new rb_purchase_persist().getLastPK(); 
                //= VarcharUpdater.getInstance().getValue(lastPK);
       // purchase.setPurchase_invoice_no(lastPK);
        rb_raw_mat_stock stock_current = (rb_raw_mat_stock)sess.get(rb_raw_mat_stock.class, purchase.getRaw_mat_id());
        int newstock = stock_current.getCurrent_stock()+purchase.getQuantity();
        double unitPrice = purchase.getUnit_cost();
        double value = unitPrice*newstock;
        System.out.println("got the values and updates willl be - "+newstock+"==="+unitPrice+"====="+value);
        //stock_current.setRaw_mat_id(stock_current.getRaw_mat_id());
        stock_current.setCurrent_unit_price(unitPrice);
        stock_current.setLast_updated_date(purchase.getDate());
        stock_current.setCurrent_stock(newstock);
        stock_current.setCurrent_value(value);
        stock_current.setLast_updated_invoice_no(lastPK);
        sess.update(stock_current);
        tx.commit();
        System.out.println("done updation");
        return true;
    }
    public static void main(String[] args){
        
        rb_purchase pur1 = new rb_purchase();
        pur1.setPayment_mode("Cash");
        pur1.setUnit_cost(2);
        Date date = new Date();
        pur1.setDate(date);
        pur1.setAmt_paid(8);
        pur1.setQuantity(13);
        pur1.setVendor_id("V1");
        pur1.setRaw_mat_id("R4");
        pur1.setRemarks("test");
        pur1.setTotal_amt(12);
        
        rb_raw_mat_stock_persist test = rb_raw_mat_stock_persist.getInstance();
        test.update(pur1);
    }
    
}
